'use strict';

/* Controllers */
app.controller('houseOwnerLoanListCtl', function($scope,$rootScope, $compile, ngDialog, uiLoad, rest, $ocLazyLoad, $state, user, $http) {
    var rentTypeObj = {
        0: "年付",
        1: "季付",
        2: "月付"
    };
    var applyTypeObj = {
        0: "支付包",
        1: "微信",
        2: "现金"
    };
    $scope.checkType = {};
    $scope.checkStatus = {};
    $scope.decorateStatus = {};
    $scope.checkTypeArray = [{
        "name": "全部",
        "value": -1
    }, {
        "name": "未申请",
        "value": -2
    }, {
        "name": "申请中",
        "value": 0
    }, {
        "name": "申请成功",
        "value": 1
    }, {
        "name": "申请失败",
        "value": 2
    }, {
        "name": "补件",
        "value": 5
    }];

    $scope.lockTypeArray = [{
        "name": "全部",
        "value": -1
    }, {
        "name": "未安装",
        "value": 0
    }, {
        "name": "锁电安装中",
        "value": 3
    }, {
        "name": "锁电安装成功",
        "value": 4
    }];

    if ($scope.queryParams) {

    }
    $scope.queryParams = {
        'checkStatus': $scope.checkStatus.selected,
        'contractStartDate': $scope.contractStartDate,
        'contractEndDate': $scope.contractEndDate,
        'nameCode': $scope.nameCode
    }


    //监听浏览器大小变化
    // window.onresize = function(){
    //     window.location.reload();
    // }

    $scope.showApplyRent = function() {
        rest.java({
            url: '/decorate-service/decorate/api/service/getCreditLine.json',
            data: {
                originalId: user.get('openId'),
                // originalId: "b8bf8e32ca823088833d2f7a0ba1fae2",
            },
            success: function(res) {
                if (res.code == '0') {
                    //实际
                    $scope.receivedAmount = res.res.data.receivedAmount;
                    //申请中
                    $scope.applySumAmount = res.res.data.applySumAmount;
                    //可申请
                    $scope.availableCreditLine = res.res.data.availableCreditLine;
                    //最大
                    $scope.creditLine = res.res.data.creditLine;
                    $scope.$apply();
                } else {
                    //alert( res.res.msg )
                }
            },
            error: function() {

            }

        });
    }
    $scope.showApplyRent();

    $scope.$watch('nameCode', function(newValue, oldValue) {
        $scope.nameCode = newValue;
    });

    $scope.queryUser = function() {
        if ($scope.queryParams.checkStatus && $scope.queryParams.checkStatus.selected) {
            var checkStatusCache = $scope.queryParams.checkStatus.selected;
            if ($scope.queryParams.checkStatus.selected) {
                $scope.queryParams.checkStatus = $scope.queryParams.checkStatus.selected.value
            }
            $scope.queryParams = {
                //'checkStatus': $scope.checkStatus.selected,
                'contractStartDate': $scope.contractStartDate,
                'contractEndDate': $scope.contractEndDate,
                'nameCode': $scope.nameCode
            }
            console.log($scope.queryParams);
            $scope.dataTable.fnClearTable();
            $scope.queryParams.checkStatus = {};
            $scope.queryParams.checkStatus.selected = {};
            $scope.queryParams.checkStatus.selected = checkStatusCache;
        } else if ($scope.queryParams.decorateStatus && $scope.queryParams.decorateStatus.selected) {
            var decorateStatusCache = $scope.queryParams.decorateStatus.selected;
            if ($scope.queryParams.decorateStatus.selected) {
                $scope.queryParams.decorateStatus = $scope.queryParams.decorateStatus.selected.value
            }
            $scope.queryParams = {
                //'decorateStatus': $scope.decorateStatus.selected,
                'contractStartDate': $scope.contractStartDate,
                'contractEndDate': $scope.contractEndDate,
                'nameCode': $scope.nameCode
            }
            console.log($scope.queryParams);
            $scope.dataTable.fnClearTable();
            $scope.queryParams.decorateStatus = {};
            $scope.queryParams.decorateStatus.selected = {};
            $scope.queryParams.decorateStatus.selected = decorateStatusCache;
        } else {
            $scope.queryParams = {
                // 'checkStatus': $scope.decorateStatus.selected,
                'contractStartDate': $scope.contractStartDate,
                'contractEndDate': $scope.contractEndDate,
                'nameCode': $scope.nameCode
            }
            console.log($scope.queryParams);
            $scope.dataTable.fnClearTable();
        }
    };

    $scope.initDataTable = function() {
        $scope.tableLoanDatas = {
            "sAjaxSource": "4008",
            "dom": "tpr",
            "bAutoWidth": true,
            "aoColumns": [
                // {
                //     "mDataProp": "index",
                //     "sTitle": "门锁ID",
                //     "sDefaultContent": "",
                //     "sClass": "center"
                // }, 
                {
                    "mDataProp": "devProducer",
                    "sTitle": "设备厂商",
                    "sDefaultContent": "",
                    "sClass": "center"
                },
                // {
                //     "mDataProp": "countyName",
                //     "sTitle": "区县",
                //     "sDefaultContent": "",
                //     "sClass": "center"
                // },
                {
                    "mDataProp": "areaName",
                    "sTitle": "小区",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "buildingNo",
                    "sTitle": "楼栋",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "roomCode",
                    "sTitle": "房间号",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, 
                // {
                //     "mDataProp": "roomCode",
                //     "sTitle": "门牌号",
                //     "sDefaultContent": "",
                //     "sClass": "center"
                // },
                 {
                    "mDataProp": "tenantsCount",
                    "sTitle": "绑定用户",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "tdStatus",
                    "sTitle": "门锁电量",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "doorLockStatus",
                    "sTitle": "门锁状态",
                    "sDefaultContent": "",
                    "sClass": "center"
                },
                // 小艳说：提现额度隐藏，以后要有一个提现额度总值
                // }, {
                //     "mDataProp": "assessAmount",
                //     "sTitle": "提现额度",
                //     "sDefaultContent": "",
                //     "sClass": "center"
                {
                    "mDataProp": "operate",
                    "sTitle": "操作",
                    "sDefaultContent": "",
                    "sClass": "center handle-td operation"
                }
            ],
            "fnRowCallback": function(nRow, aData, iDisplayIndex) {
                var btnStr = "";

                btnStr += "<div class='btn btn-new' ui-toggle-class='active' target='.asideDetails' ng-click='viewElectricDetails(\"" + aData.houseId + "\")'>查看详情</div>"
               
                function time(mydate) {
                    var str = mydate.substring(0, 10);
                    var dataArr = str.split('-');
                    var timeStr = dataArr[0] + "年" + dataArr[1] + "月" + dataArr[2] + "日";
                    return timeStr;
                }
                if (aData.decorateStatus == 4) {
                    var installStatus1 = "已安装";
                } else {
                    var installStatus1 = "未安装";
                }
                // btnStr += "<a href='javascript:;' class='btn btn-yellow' ng-click='cancelReturnProOrder(" + aData.houseId + "," + aData.orderId +")' style='margin-right:10px;' >补件</a>";
                $(nRow).find(".handle-td").append($compile(btnStr)($scope));
                $(nRow).find(".handle-td>a.operation-btn").on('click', function(e) {
                    e.preventDefault();
                    var sopt = $(this).parent().find(".operation-loan");
                    if (sopt.css('display') == 'none') {
                        $(".operation-loan").hide();
                        sopt.show();
                    } else {
                        sopt.hide();
                    }
                    return false;
                });
                if (aData.createDate != null && aData.checkDate != null) {
                    $(nRow).find(".submit-time").empty().append(time(aData.createDate));
                    $(nRow).find(".checkout-time").empty().append(time(aData.checkDate));
                }
                // console.log(aData);
                $scope.houseId = aData.houseId;
            },
            "fnDrawCallback": function(oSettings) {
                $scope.checkStatusValue = $scope.queryParams.checkStatus;
                if ("number" == typeof $scope.checkStatusValue) {
                    $scope.checkTypeArray.map(function(ele, index) {
                        if ($scope.checkStatusValue == ele.value) {
                            $scope.checkStatus.selected = ele;
                        }
                    })
                }

                $scope.decorateStatusValue = $scope.queryParams.decorateStatus;
                if ("number" == typeof $scope.decorateStatusValue) {
                    $scope.lockTypeArray.map(function(ele, index) {
                        if ($scope.decorateStatusValue == ele.value) {
                            $scope.decorateStatus.selected = ele;
                        }
                    })
                }
            },
            "fnServerParams": function(aoData) {
                if ($scope.checkStatus && $scope.checkStatus.selected) {
                    $scope.queryParams.checkStatus = $scope.checkStatus.selected.value
                }
                if ($scope.decorateStatus && $scope.decorateStatus.selected) {
                    $scope.queryParams.decorateStatus = $scope.decorateStatus.selected.value
                }
                aoData.push({
                    'name': 'queryParams',
                    'value': $scope.queryParams
                });
                return aoData;
            }
        };
    }

    $scope.initDataTable();

    $scope.dataTable = undefined;

    $scope.viewElectricDetails = function(houseId) {
        $rootScope.details = houseId;
        // console.log($rootScope.details);
        // $(".backModel").prop('display', 'block');
        document.getElementById('backModel').style.display = 'block';
    }

    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }
    $('#example thead').on('click', 'th', function() {
        // var data = $('#example').DataTable().row( this ).data();
        // alert('You clicked on  row');
    });
    $('#example tbody').on('click', 'span#edit', function() {
        var data = $('#example').DataTable().row($(this).parents('tr'));
        alert("查看修改：" + data[0]);
    });
    $('#example tbody').on('click', 'span#del', function() {
        // var data = $('#example').DataTable().row($(this).parents('tr')).data();
    });

    $("#backModel").on('click', function(event) {
        document.getElementById('backModel').style.display = 'none';
        $('.asideDetails').removeClass("active");
    });

    $("#backModel-x").on('click', function(event) {
        document.getElementById('backModel').style.display = 'none';
        $('.asideDetails').removeClass("active");
    });

});


app.controller('viewElectricDetailsCtrl', function($scope, user, rest, tipService, $location, $rootScope) {
    $scope.lockTypeArray = [{
        "name": "全部",
        "value": -1
    }, {
        "name": "解绑用户",
        "value": 0
    }, {
        "name": "充值100元",
        "value": 3
    }, {
        "name": "绑定用户",
        "value": 4
    }];
    $rootScope.$watch('details', function(newValue, oldValue) {
        console.log(newValue)
    });
    $scope.initDataTable = function() {
        $scope.tableLoanDatas = {
            "sAjaxSource": "4008",
            "dom": "tpr",
            "bAutoWidth": true,
            "aoColumns": [

                {
                    "mDataProp": "devProducer",
                    "sDefaultContent": "",
                    "sClass": "center"
                },
                {
                    "mDataProp": "areaName",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "roomCode",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, 
                
            ],            
        };
    }

    $scope.initDataTable();

})